package com.bhy702.generator.builder.util;


import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.sql.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author: brbai
 * @create: 2019-12-14 19:16:14
 * @description: 数据库工具类
 */
public class DBUtil {

    /**
     * @Description: 获取数据库表结构
     * @Param: [sqlSessionFactory]
     * @return: java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.lang.String>>
     */
    public static Map<String, Map<String, String>> getDatabaseAttribute(SqlSession sqlSession) throws SQLException {

        Map<String, Map<String, String>> databaseAttribute = new HashMap<>();

        Connection connection = sqlSession.getConnection();

        //获得数据库信息
        DatabaseMetaData metaData = connection.getMetaData();

        String databaseName = metaData.getDatabaseProductName();

        if ("MySQL".equals(databaseName)) {
            //获得数据库表
            ResultSet resultSet = metaData.getTables(null, null, null, new String[]{"TABLE"});
            while (resultSet.next()) {
                String tableName = resultSet.getString(3);
                String sql = "SELECT * FROM " + tableName;
                PreparedStatement preparedStatement = connection.prepareStatement(sql);
                ResultSet set = preparedStatement.executeQuery();
                //获得表信息
                ResultSetMetaData md = set.getMetaData();
                //获得表字段数
                int columnCount = md.getColumnCount();

                Map<String, String> attributes = new LinkedHashMap<>();

                for (int i = 1; i <= columnCount; i++) {
                    //字段名
                    String columnName = md.getColumnName(i);
                    //字段属性
                    String columnType = md.getColumnTypeName(i);

                    attributes.put(columnName, columnType);
                }
                databaseAttribute.put(tableName,attributes);
            }
        }else if("Oracle".equals(databaseName)){
                ResultSet rs = metaData.getTables("null", "DEVTEST", "%", new String[]{"TABLE"});

                while (rs.next()) {
                    String tableName = rs.getString("TABLE_NAME");
                    ResultSet columns = metaData
                            .getColumns(null, "DEVTEST", tableName, "%");

                    Map<String, String> attributes = new LinkedHashMap<>();
                    while (columns.next()) {
                        attributes.put(columns.getString("COLUMN_NAME"), columns.getString("TYPE_NAME"));
                    }
                    databaseAttribute.put(tableName, attributes);
                }
        }


        return databaseAttribute;
    }


    /**
     * @Description: 数据库字段类型转换
     * @Param: [sqlType]
     * @return: java.lang.String
     */
    public static String sqlTypeToJavaType(String sqlType) {

        if(sqlType.equalsIgnoreCase("bit")){
            return "boolean";
        }else if(sqlType.equalsIgnoreCase("tinyint")){
            return "byte";
        }else if(sqlType.equalsIgnoreCase("smallint")){
            return "short";
        }else if(sqlType.equalsIgnoreCase("int")){
            return "int";
        }else if(sqlType.equalsIgnoreCase("bigint")){
            return "long";
        }else if(sqlType.equalsIgnoreCase("float")){
            return "float";
        }else if(sqlType.equalsIgnoreCase("decimal") || sqlType.equalsIgnoreCase("numeric")
                || sqlType.equalsIgnoreCase("real") || sqlType.equalsIgnoreCase("money")
                || sqlType.equalsIgnoreCase("smallmoney")|| sqlType.equalsIgnoreCase("double")){
            return "double";
        }else if(sqlType.equalsIgnoreCase("varchar") || sqlType.equalsIgnoreCase("char")
                || sqlType.equalsIgnoreCase("nvarchar") || sqlType.equalsIgnoreCase("nchar")
                || sqlType.equalsIgnoreCase("text")){
            return "String";
        }else if(sqlType.equalsIgnoreCase("datetime")){
            return "Date";
        }else if(sqlType.equalsIgnoreCase("image")){
            return "Blod";
        }

        return null;
    }

    /**
     * @Description: Oracle数据库字段类型转换
     * @Param: [sqlType]
     * @return: java.lang.String
     */
    public static String oracleSqlTypeToJavaType(String sqlType) {

        if(sqlType.equalsIgnoreCase("binary_double")){
            return "double";
        }else if(sqlType.equalsIgnoreCase("binary_float")){
            return "float";
        }else if(sqlType.equalsIgnoreCase("blob")){
            return "byte[]";
        }else if(sqlType.equalsIgnoreCase("blob")){
            return "byte[]";
        }else if(sqlType.equalsIgnoreCase("char") || sqlType.equalsIgnoreCase("nvarchar2")
                || sqlType.equalsIgnoreCase("varchar2")){
            return "String";
        }else if(sqlType.equalsIgnoreCase("date") || sqlType.equalsIgnoreCase("timestamp")
                || sqlType.equalsIgnoreCase("timestamp with local time zone")
                || sqlType.equalsIgnoreCase("timestamp with time zone")){
            return "Date";
        }else if(sqlType.equalsIgnoreCase("number")){
            return "Long";
        }

        return "String";
    }
}
